﻿using System;
using System.Windows;

using Supremacy.Diplomacy;
using Supremacy.Economy;
using Supremacy.Game;

namespace Supremacy.Client
{
    /// <summary>
    /// Interaction logic for GiftEditor.xaml
    /// </summary>
    public partial class GiftEditor
    {
        private const int SliderSteps = 20;

        private readonly CivilizationManager _civManager;

        public GiftEditor(GiftStipulation stipulation, CivilizationManager civManager)
        {
            if (stipulation == null)
                throw new ArgumentNullException("stipulation");
            this._civManager = civManager;
            this.DataContext = stipulation;
            InitializeComponent();
            InitializeRanges();
        }

        private void InitializeRanges()
        {
            if (_civManager == null)
                return;

            int stepSize;
            int numSteps;

            stepSize = (_civManager.Credits.CurrentValue / SliderSteps);
            if (stepSize == 0)
            {
                stepSize = 1;
                CreditsSlider.Maximum = _civManager.Credits.CurrentValue;
            }
            else
            {
                numSteps = (stepSize == 0) ? 0 : Math.Min(_civManager.Credits.CurrentValue / stepSize, SliderSteps);
                CreditsSlider.Maximum = stepSize * numSteps;
            }
            CreditsSlider.SmallChange = stepSize;
            CreditsSlider.LargeChange = stepSize;

            stepSize = (_civManager.Resources[ResourceType.RawMaterials].CurrentValue / SliderSteps);
            if (stepSize == 0)
            {
                stepSize = 1;
                RawMaterialsSlider.Maximum = _civManager.Resources[ResourceType.RawMaterials].CurrentValue;
            }
            else
            {
                numSteps = (stepSize == 0) ? 0 : Math.Min(_civManager.Resources[ResourceType.RawMaterials].CurrentValue / stepSize, SliderSteps);
                RawMaterialsSlider.Maximum = stepSize * numSteps;
            }
            RawMaterialsSlider.SmallChange = stepSize;
            RawMaterialsSlider.LargeChange = stepSize;

            stepSize = (_civManager.Resources[ResourceType.Deuterium].CurrentValue / SliderSteps);
            if (stepSize == 0)
            {
                stepSize = 1;
                DeuteriumSlider.Maximum = _civManager.Resources[ResourceType.Deuterium].CurrentValue;
            }
            else
            {
                numSteps = (stepSize == 0) ? 0 : Math.Min(_civManager.Resources[ResourceType.Deuterium].CurrentValue / stepSize, SliderSteps);
                DeuteriumSlider.Maximum = stepSize * numSteps;
            }
            DeuteriumSlider.SmallChange = stepSize;
            DeuteriumSlider.LargeChange = stepSize;

            stepSize = (_civManager.Resources[ResourceType.Dilithium].CurrentValue / SliderSteps);
            if (stepSize == 0)
            {
                stepSize = 1;
                DilithiumSlider.Maximum = _civManager.Resources[ResourceType.Dilithium].CurrentValue;
            }
            else
            {
                numSteps = (stepSize == 0) ? 0 : Math.Min(_civManager.Resources[ResourceType.Dilithium].CurrentValue / stepSize, SliderSteps);
                DilithiumSlider.Maximum = stepSize * numSteps;
            }
            DilithiumSlider.SmallChange = stepSize;
            DilithiumSlider.LargeChange = stepSize;
        }

        private void AcceptButton_Click(object sender, RoutedEventArgs e)
        {
            if (((CreditsSlider.Value > 0)
                 || (RawMaterialsSlider.Value > 0)
                 || (DeuteriumSlider.Value > 0)
                 || (DilithiumSlider.Value > 0))
                && (this.DurationEditor.Duration != Stipulation.InvalidDuration))
            {
                Window window = this.Parent as Window;
                if (window != null)
                {
                    window.DialogResult = true;
                }
            }
        }
    }
}
